#include "static_data_svc.h"
#include <wf_task/tasks.h>
#include "bthread_task.h"
#define SimpleCallBack                                                       \
    brpc::Controller *cntl = static_cast<brpc::Controller *>(cntl_base);     \
    auto cb = [done, cntl](int errorcode, const char *errorstr) {            \
        if (errorcode)                                                       \
        {                                                                    \
            cntl->SetFailed(errorcode, "fetch database error:%s", errorstr); \
        }                                                                    \
        done->Run();                                                         \
    }
namespace frpcType
{

    void StaticDataSvcImpl::GetExchangeList(google::protobuf::RpcController *cntl_base,
                                        const DefaultArg *request,
                                        ExchangeArray *response,
                                        google::protobuf::Closure *done)
    {

         brpc::ClosureGuard done_guard(done);
        SimpleCallBack;
        WFTask::GetExchangeList(response, cb);
        done_guard.release();
    }

    void StaticDataSvcImpl::GetCollectionList(google::protobuf::RpcController *cntl_base, const DefaultArg *request, CollectionArray *response, google::protobuf::Closure *done)
    {
        brpc::ClosureGuard done_guard(done);
        SimpleCallBack;
        auto ctx = cntl->auth_context();
        WFTask::GetCollectionList((uint64_t *)ctx->user().data(), response, cb);
        done_guard.release();
    }

    void StaticDataSvcImpl::GetCollectionContent(google::protobuf::RpcController *cntl_base, const DefaultArg *request, ContractArray *response, google::protobuf::Closure *done)
    {
        brpc::ClosureGuard done_guard(done);
        SimpleCallBack;
        auto ctx = cntl->auth_context();
        WFTask::GetCollectionContent((uint64_t *)ctx->user().data(), request, response, cb);
        done_guard.release();
    }

    void StaticDataSvcImpl::GetBarSeries(google::protobuf::RpcController *cntl_base, const GetBarRequest *request, DefaultArg *response, google::protobuf::Closure *done)
    {
        brpc::ClosureGuard done_guard(done);
        auto task = new ReadBarTask;
        task->done = done;
        task->request = request;
        brpc::Controller *cntl = static_cast<brpc::Controller *>(cntl_base);
        task->response = response;
        task->cntl = cntl;
        bthread_t th;
        CHECK_EQ(0, bthread_start_background(&th, NULL, process_thread, task));
        done_guard.release();
    }

   
    void StaticDataSvcImpl::GetBarRange(google::protobuf::RpcController* cntl_base, const GetBarRequest* request, DefaultArg* response, google::protobuf::Closure* done)
    {
        brpc::ClosureGuard done_guard(done);
        auto task = new ReadBarRangeTask;
        task->done = done;
        task->request = request;
        brpc::Controller* cntl = static_cast<brpc::Controller*>(cntl_base);
        task->response = response;
        task->cntl = cntl;
        bthread_t th;
        CHECK_EQ(0, bthread_start_background(&th, NULL, process_thread, task));
        done_guard.release();
    }

}